<?php
/**
 * Created by PhpStorm.
 * User:吴鑫
 */

namespace backend\service;

use common\models\GoodsInfo;
use common\models\Order;
use common\models\OrderStatusLog;
use common\service\BusinessService;
use common\service\ConstatService;
use yii\db\Query ;
use yii\data\Pagination ;
use Yii ;

class OrderService
{

    /**
     * 订单列表
     * @param $limit
     * @param null $searchData
     * @param null $orderby
     * @return array
     */
    public static function findOrderList($limit,$searchData=null,$orderby=null,$status=null){
        $pages = null ;
        $query = new Query();

        $query->select('*')
                ->from(Order::tableName()) ;

        if(!empty($searchData['store']) && $searchData['store']!=-1){
            $query->andWhere(["store_code"=>$searchData['store']]) ;
        }

        if(!empty($searchData['source']) && $searchData['source']!=-1){
            $query->andWhere(["source"=>$searchData['source']]) ;
        }

        if(!empty($status)){
            $query->andWhere(["status"=>$status]) ;
        }else{
            if(!empty($searchData['status']) && $searchData['status']!=-1){
                $query->andWhere(["status"=>$searchData['status']]) ;
            }
        }

        if(!empty($searchData['sub_status']) && $searchData['sub_status']!=-1){
            $query->andWhere(["sub_status"=>$searchData['sub_status']]) ;
        }

        if(!empty($searchData['orderno'])){
            $query->andWhere(["orderno"=>$searchData['orderno']]) ;
        }

        if(!empty($searchData['user_phone'])){
            $query->andWhere(["user_phone"=>$searchData['user_phone']]) ;
        }

        if(!empty($searchData['sender_phone'])){
            $query->andWhere(["sender_phone"=>$searchData['sender_phone']]) ;
        }

        if(!empty($searchData['send_user_name'])){
           // $query->andWhere(["send_user_name"=>$searchData['send_user_name']]) ;
        }

        if(!empty($searchData['expectpick_time_strart'])){
            $query->andWhere([">=","expectpick_time",$searchData['expectpick_time_strart']]) ;
        }

        if(!empty($searchData['expectpick_time_end'])){
            $query->andWhere(["<","expectpick_time",$searchData['expectpick_time_end']]) ;
        }

        if(!empty($searchData['expect_time_strart'])){
            $query->andWhere([">=","expect_time",$searchData['expect_time_strart']]) ;
        }

        if(!empty($searchData['expect_time_end'])){
            $query->andWhere(["<","expect_time",$searchData['expect_time_end']]) ;
        }

        if(!empty($searchData['sign_time_strart'])){
            $query->andWhere([">=","sign_time",$searchData['sign_time_strart']]) ;
        }

        if(!empty($searchData['sign_time_end'])){
            $query->andWhere(["<","sign_time",$searchData['sign_time_end']]) ;
        }

        if(!empty($searchData['business_type']) && $searchData['business_type']!=-1){
            $query->andWhere(["business_type" => $searchData['business_type']]) ;
        }

        if(!empty($orderby)){
            $query->orderBy($orderby) ;
        }else{
            $query->orderBy("id desc") ;
        }

        if(isset($limit)){
            $pages = new Pagination(['totalCount' =>$query->count(), 'defaultPageSize' => $limit]);
            $query->offset($pages->offset)
                ->limit($limit) ;
        }

        $dataList = $query->all() ;

        return ["list" => $dataList, "page" => $pages];

    }

    /**
     * 导出订单
     * @param null $searchData
     * @return array
     */
    public static function exportOrder($searchData=null,$orderby=null,$status=null){
        $pages = null ;
        $query = new Query();

        $query->select('*')
            ->from(Order::tableName()) ;

        if(!empty($searchData['store']) && $searchData['store']!=-1){
            $query->andWhere(["store_code"=>$searchData['store']]) ;
        }

        if(!empty($searchData['source']) && $searchData['source']!=-1){
            $query->andWhere(["source"=>$searchData['source']]) ;
        }

        if(!empty($status)){
            $query->andWhere(["status"=>$status]) ;
        }else{
            if(!empty($searchData['status']) && $searchData['status']!=-1){
                $query->andWhere(["status"=>$searchData['status']]) ;
            }
        }

        if(!empty($searchData['sub_status']) && $searchData['sub_status']!=-1){
            $query->andWhere(["sub_status"=>$searchData['sub_status']]) ;
        }

        if(!empty($searchData['orderno'])){
            $query->andWhere(["orderno"=>$searchData['orderno']]) ;
        }

        if(!empty($searchData['user_phone'])){
            $query->andWhere(["user_phone"=>$searchData['user_phone']]) ;
        }

        if(!empty($searchData['sender_phone'])){
            $query->andWhere(["sender_phone"=>$searchData['sender_phone']]) ;
        }

        if(!empty($searchData['send_user_name'])){
            // $query->andWhere(["send_user_name"=>$searchData['send_user_name']]) ;
        }

        if(!empty($searchData['expectpick_time_strart'])){
            $query->andWhere([">=","expectpick_time",$searchData['expectpick_time_strart']]) ;
        }

        if(!empty($searchData['expectpick_time_end'])){
            $query->andWhere(["<","expectpick_time",$searchData['expectpick_time_end']]) ;
        }

        if(!empty($searchData['expect_time_strart'])){
            $query->andWhere([">=","expect_time",$searchData['expect_time_strart']]) ;
        }

        if(!empty($searchData['expect_time_end'])){
            $query->andWhere(["<","expect_time",$searchData['expect_time_end']]) ;
        }

        if(!empty($searchData['sign_time_strart'])){
            $query->andWhere([">=","sign_time",$searchData['sign_time_strart']]) ;
        }

        if(!empty($searchData['sign_time_end'])){
            $query->andWhere(["<","sign_time",$searchData['sign_time_end']]) ;
        }

        if(!empty($searchData['business_type']) && $searchData['business_type']!=-1){
            $query->andWhere(["business_type" => $searchData['business_type']]) ;
        }

        if(!empty($orderby)){
            $query->orderBy($orderby) ;
        }else{
            $query->orderBy("id desc") ;
        }

        $count = $query->count() ;

        return ["query" => $query, "count" => $count];

    }

    /**
     * 订单详情
     * @param $orderno
     * @return array
     */
    public static function detail($orderno){
        if(empty($orderno)){
            return ["code"=>400,"msg"=>"订单号不能空"];
        }

        $order = Order::find()->where(["orderno"=>$orderno])->asArray()->one() ;

        if(empty($order)){
            return ["code"=>400,"msg"=>"不存在该订单"];
        }

        $orderStatusLog = OrderStatusLog::find()
            ->where(["orderno"=>$orderno])
            ->orderBy("id asc")
            ->all() ;

        $goodsInfos = GoodsInfo::find()
            ->where(["orderno"=>$orderno])
            ->orderBy("id asc")
            ->all() ;

        $data = [
            "order"=>$order ,
            "orderStatusLog" => $orderStatusLog ,
            "goodsInfos" => $goodsInfos ,
        ] ;
        return ["code"=>200,"msg"=>"成功","data"=>$data] ;
    }

    /**
     * 修改状态
     * @param $orderno
     * @param $status
     * @return array
     */
//    public static function changeStatus($orderno,$status,$sub_status=null){
//        if(empty($orderno)){
//            return ["code"=>400,"msg"=>"订单号不能空"];
//        }
//
//        $order = Order::findOne(["orderno"=>$orderno]) ;
//
//        if(empty($order)){
//            return ["code"=>400,"msg"=>"不存在该订单"];
//        }
//
//        $oldStatus = $order['status'] ;
//
//        $statusArr = ConstatService::ORDER_STATUS_DESC ;
//
//        if(empty($statusArr[$status])){
//            return ["code"=>400,"msg"=>"状态不合法"];
//        }
//
//        if(!empty(Yii::$app->admin->identity->store_code)){
//            if(Yii::$app->admin->identity->store_code!=$order['store_code']){
//                return ["code"=>400,"msg"=>"无法操作其它门店的数据"];
//            }
//        };
//
//        if($status==1){
//            if($oldStatus==1){
//                return ["code"=>400,"msg"=>"状态一致不用修改"];
//            }
//
//            $order->status = $status ;
//            $order->sender_name = '' ;
//            $order->sender_phone  =  '' ;
//            $order->sender_user_id = null ;
//            $order->receive_data_time = null ;
//            $order->receive_entity_time = null ;
//            $order->dispatching_time = null ;
//            $order->cancel_time = null ;
//            $order->exception_time = null ;
//            $order->sign_time = null ;
//            $rs = $order->save() ;
//            if(empty($rs)){
//                $msg = current($order->getFirstErrors());
//                return ["code"=>400,"msg"=>"失败,原因:".$msg];
//            }
//
//            $logArr['orderno'] = $orderno;
//            $logArr['status'] = $status;
//            BusinessService::addOrderlog($logArr) ;
//
//            $backendLogArr['orderno'] = $orderno;
//            $backendLogArr['old_status'] = $oldStatus;
//            $backendLogArr['status'] = $status;
//            BusinessService::addBackendOrderlog($backendLogArr);
//
//            return ["code"=>200,"msg"=>"成功"];
//        }
//
//    }


    /**
     * 删除订单
     * @param $orderno
     * @return array
     */
    public static function delOrder($orderno){
        if(empty($orderno)){
            return ["code"=>400,"msg"=>"订单号不能空"];
        }

        $order =  Order::findOne(["orderno"=>$orderno]) ;

        if(empty($order)){
            return ["code"=>400,"msg"=>"不存在该订单"];
        }

        if($order->status!=ConstatService::ORDER_DISTRIBUTION) {
            return ["code"=>400,"msg"=>"该订单状态不为待取，无法删除"];
        }

        $order->delete() ;

        return ["code"=>200,"msg"=>"成功"] ;
    }


}